package common.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import common.model.User;
import login.LoginService;

public class UserInterceptor implements Interceptor {

    public void intercept(Invocation inv) {
        Controller c = inv.getController();
        User user = LoginService.me.validateToken(c.getCookie(LoginService.USER));
        if (null != user) {
            if (user.getAdmin()) {
                c.setAttr("manager", user);
            }
            if (user.getLevel().equals("部门经理")) {
                c.setAttr("admin", user);
            }
            c.setAttr("user", user);


            c.setAttr("username", user.getUsername() + user.getLevel());

            inv.invoke();
        } else {
            inv.invoke();
            c.redirect("/login");
        }
    }
}
